package com.example.administrator.baikangxing.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.config.Constants;
import com.nineoldandroids.animation.ObjectAnimator;

public class DeclareActivity extends BaseActivity {

    private ImageView id_ivBall;
    private ImageView id_Line;
    private TextView declare_num;
    private TextView declare_time;

    @Override
    public int getContentViewLayoutId() {
        return R.layout.activity_declare_activity;
    }

    @Override
    public void initView() {
        base_ll_title.setBackgroundResource(R.color.heart_bg);
        setTitles("心率说明");
        base_ib_menu.setVisibility(View.INVISIBLE);
        id_ivBall = findView(R.id.id_ivBall);
        id_Line = findView(R.id.id_Line);
        declare_num = findView(R.id.declare_tv_num);
        declare_time = findView(R.id.declare_tv_time);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        String heart_num = intent.getStringExtra(Constants.HEART_NUM);
        String heart_time = intent.getStringExtra(Constants.HEART_TIME);
        setView(Integer.parseInt(heart_num));
        declare_num.setText(heart_num);
        declare_time.setText(heart_time);
    }

    private void setView(int value) {
        int widthLine = getWidth(id_Line);//线的总宽度
        int pos[] = getLocation(id_ivBall);//球的位置
        int perWidth = widthLine / 4;//线平分为4段

        if (value <= 90) {
            int perPx = perWidth / 90;//每个像素对应的值value
            if (perPx == 0) perPx = 1;
            int movePx = value * perPx;//球要移动的距离
            if (value == 90) movePx = perWidth - pos[0] / 2;

            ObjectAnimator.ofFloat(id_ivBall, "translationX", movePx).setDuration(1000).start();
//            translateAnim(pos[0],pos[1],movePx,pos[1]);
            Log.e("lym", "widthLine==" + widthLine + "pos[0]==" + pos[0] + "pos[1]===" + pos[1] + "perWidth==" + perWidth + "perPx==" + perPx + "movePx==" + movePx);
            //[lym]:widthLine==0pos[0]==23perWidth==0perPx==0movePx==-23
            // [lym]:widthLine==475pos[0]==23perWidth==118perPx==1movePx==70
            //[lym]:widthLine==475pos[0]==23pos[1]===663perWidth==118perPx==1movePx==70

        } else if (value <= 127 && value > 90) {
            int perPx = perWidth / 127;//每个像素对应的值value
            if (perPx == 0) perPx = 1;
            int movePx = value * perPx + perWidth - pos[0];//球要移动的距离
            if (value == 91) movePx = perWidth - pos[0] / 2;
            else if (value == 127)
                movePx = perWidth * 2 - pos[0] / 2;

            ObjectAnimator.ofFloat(id_ivBall, "translationX", movePx).setDuration(1000).start();
            Log.e("lym", "widthLine==" + widthLine + "pos[0]==" + pos[0] + "pos[1]===" + pos[1] + "perWidth==" + perWidth + "perPx==" + perPx + "movePx==" + movePx);
        } else {
            int perPx = perWidth / 155;//每个像素对应的值value
            if (perPx == 0) perPx = 1;
            int movePx = value * perPx + perWidth * 2 - pos[0] * 2;//球要移动的距离
            if (value == 128)
                movePx = perWidth * 2;
            else if (value == 155)
                movePx = perWidth * 3;
            else if (value > 155)
                movePx = perWidth * 3 + value - 125;
            if (value >= widthLine)
                movePx = widthLine;
            ObjectAnimator.ofFloat(id_ivBall, "translationX", movePx).setDuration(1000).start();
            Log.e("lym", "widthLine==" + widthLine + "pos[0]==" + pos[0] + "pos[1]===" + pos[1] + "perWidth==" + perWidth + "perPx==" + perPx + "movePx==" + movePx);
        }
    }

    //获取控件的宽
    private int getWidth(ImageView view) {
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int height = view.getMeasuredHeight();
        int width = view.getMeasuredWidth();
        return width;
    }

    //获取ImageView控件所在的位置
    private int[] getLocation(ImageView view) {
        int[] position = new int[2];
        view.getLocationInWindow(position);
        return position;
    }
}
